﻿using System.Collections.Generic;
using System.IO;
using Aptea.OpenXmlOffice;
using eCRAM.Dao;
using eCRAM.Model;
using eCRAM.Tools;

namespace eCRAM.Bll
{
    public class CramExcelFileGenerator
    {

        /// <summary>
        /// Cram contentant les données à exporter
        /// </summary>
        public Cram Cram { get; private set; }

        #region Parameters
        public string DestinationFolder
        {
            get { return ParameterRepository.GetByEnum(EnumParameter.RepertoireExportExcel).Value; }
        }

        public int NbProject
        {
            get { return int.Parse(ParameterRepository.GetByEnum(EnumParameter.NbProjetEsportExcel).Value); }
        }

        public int NbHolidays
        {
            get { return ActivityRepository.GetCount(a => a.ActivityType.IsHoliday); }
        }
        #endregion

        /// <summary>
        /// Nom du fichier généré
        /// </summary>
        public string FileName { get; protected set; }

        public CramExcelFileGenerator(Cram cram)
        {
            Cram = cram;
        }

        /// <summary>
        /// Genére le fichier
        /// </summary>
        public virtual void Generate()
        {
            if (!Directory.Exists(DestinationFolder))
            {
                Directory.CreateDirectory(DestinationFolder);
            }


            if (Cram != null && Cram.CramLines.Count > 0)
            {
                // Génération du cram "normal"
                FileName = Path.Combine(DestinationFolder, GetFileName(Cram.User));
                using (var excel = new ExcelManager(FileName))
                {
                    new GraphAndSaveExcelFileGenerator
                    {
                            ExcelManager = excel,
                            Cram = Cram,
                            ProjectPredicate = item => !item.Activity.ActivityType.IsHoliday && !item.Activity.IsAstreinte,
                            HolidayPredicate = item => item.Activity.ActivityType.IsHoliday,
                    }
                    .Generate(); 
                   
                }
            }
        }
        
        /// <summary>
        /// Met en majuscule la première lettre de chaque mot
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private static string CapitalizeName(string name)
        {
            var items = name.Split('-');
            var sb = new List<string>();
            foreach (var item in items)
            {
                var itemCapital = item.Capitalize();
                sb.Add(itemCapital);
            }
            return string.Join("-", sb);
        }

        /// <summary>
        /// Obtient le nom du fichier generé
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        protected virtual string GetFileName(User user)
        {
            var anneeMois = Cram.Year + "-" + Cram.Month.ToString("00");
            var tabName = user.FirstName.ToLower().Split('-');
            for (var i = 0; i < tabName.Length; i++)
            {
                tabName[i] = CapitalizeName(tabName[i]);
            }
            var firstName = string.Join("-", tabName);
            var lastName = user.LastName.ToUpperInvariant().Replace(" ", "_");

            return string.Format("{0} CRAM {1} {2}.xlsx", anneeMois, firstName, lastName);
        }
    }
}
